import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';

/**
 * ***********************************************
 * 包路径：example.lib
 * 类描述：多图预览
 * 创建人：Liu Yinglong[PHONE：132****0095]
 * 创建时间：2019/11/28
 * 修改人：
 * 修改时间：2019/11/28
 * 修改备注：
 * ***********************************************
 */
class PictureViewMultPage extends StatefulWidget {
  List<String> picUrls;
  final contentBgColor;
  final int selectIndex;

  PictureViewMultPage(this.picUrls, {this.contentBgColor = Colors.black,this.selectIndex=0});

  @override
  _PictureViewMultPageState createState() => _PictureViewMultPageState();
}

class _PictureViewMultPageState extends State<PictureViewMultPage> {
  bool verticalGallery = false;
  int selectIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    selectIndex=widget.selectIndex;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        brightness: Brightness.light, 
        title: Text(
          "${selectIndex + 1}/${widget.picUrls.length}",
          style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1),fontWeight: FontWeight.w500),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        leading: Builder(
          builder: (BuildContext context) {
            return IconButton(
              icon: const Icon(Icons.arrow_back_ios),
              color: Color.fromRGBO(25, 35, 60, 1),
              onPressed: () {
                if(Navigator.of(context).canPop()){
                  Navigator.of(context).pop();
                }
              },
            );
          },
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(top:20.0,bottom: 20.0),
        child: PhotoViewGallery.builder(
          backgroundDecoration: BoxDecoration(color: widget.contentBgColor),
          scrollPhysics: const BouncingScrollPhysics(),
          itemCount: widget.picUrls.length,
          builder: (ctx, index) {
            return PhotoViewGalleryPageOptions(
              minScale: 0.1,
              maxScale: 2.0,
              initialScale: 0.1,
              imageProvider: NetworkImage(widget.picUrls[selectIndex],scale: 0.5),
            );
          },
          onPageChanged: (index) {
            setState(() {
              selectIndex = index;
            });
          },
        ),
      ),
    );
  }
}
